<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Redis 持久化机制深度解析</title>
    <link href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            min-height: 100vh;
        }
        
        .hero-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            position: relative;
            overflow: hidden;
        }
        
        .hero-gradient::before {
            content: '';
            position: absolute;
            top: 0;
            left: 0;
            right: 0;
            bottom: 0;
            background: url("data:image/svg+xml,%3Csvg width='60' height='60' viewBox='0 0 60 60' xmlns='http://www.w3.org/2000/svg'%3E%3Cg fill='none' fill-rule='evenodd'%3E%3Cg fill='%23ffffff' fill-opacity='0.05'%3E%3Cpath d='M36 34v-4h-2v4h-4v2h4v4h2v-4h4v-2h-4zm0-30V0h-2v4h-4v2h4v4h2V6h4V4h-4zM6 34v-4H4v4H0v2h4v4h2v-4h4v-2H6zM6 4V0H4v4H0v2h4v4h2V6h4V4H6z'/%3E%3C/g%3E%3C/g%3E%3C/svg%3E");
        }
        
        .content-card {
            background: white;
            border-radius: 16px;
            box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
            transition: transform 0.3s ease, box-shadow 0.3s ease;
        }
        
        .content-card:hover {
            transform: translateY(-5px);
            box-shadow: 0 25px 50px rgba(0, 0, 0, 0.15);
        }
        
        .section-title {
            position: relative;
            padding-left: 20px;
        }
        
        .section-title::before {
            content: '';
            position: absolute;
            left: 0;
            top: 50%;
            transform: translateY(-50%);
            width: 4px;
            height: 24px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            border-radius: 2px;
        }
        
        .highlight-box {
            background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
            color: white;
            padding: 20px;
            border-radius: 12px;
            margin: 20px 0;
            position: relative;
            overflow: hidden;
        }
        
        .highlight-box::before {
            content: '';
            position: absolute;
            top: -50%;
            right: -50%;
            width: 200%;
            height: 200%;
            background: radial-gradient(circle, rgba(255,255,255,0.1) 0%, transparent 70%);
            animation: pulse 3s ease-in-out infinite;
        }
        
        @keyframes pulse {
            0%, 100% { transform: scale(1); opacity: 0.5; }
            50% { transform: scale(1.1); opacity: 0.8; }
        }
        
        .code-block {
            background: #2d3748;
            color: #e2e8f0;
            padding: 16px 20px;
            border-radius: 8px;
            font-family: 'Courier New', monospace;
            overflow-x: auto;
            position: relative;
        }
        
        .code-block::before {
            content: 'CODE';
            position: absolute;
            top: 8px;
            right: 12px;
            font-size: 10px;
            color: #718096;
            font-weight: bold;
            letter-spacing: 1px;
        }
        
        .feature-card {
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            padding: 24px;
            border-radius: 12px;
            transition: all 0.3s ease;
            border: 2px solid transparent;
        }
        
        .feature-card:hover {
            border-color: #667eea;
            transform: translateY(-3px);
            box-shadow: 0 10px 20px rgba(102, 126, 234, 0.1);
        }
        
        .mermaid {
            display: flex;
            justify-content: center;
            margin: 40px 0;
        }
        
        .drop-cap {
            float: left;
            font-size: 4em;
            line-height: 0.8;
            margin: 0.1em 0.1em 0 0;
            font-weight: 700;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            font-family: 'Noto Serif SC', serif;
        }
        
        .info-pill {
            display: inline-block;
            background: #edf2f7;
            color: #4a5568;
            padding: 4px 12px;
            border-radius: 20px;
            font-size: 14px;
            margin: 0 4px;
        }
        
        .timeline-item {
            position: relative;
            padding-left: 40px;
            margin-bottom: 30px;
        }
        
        .timeline-item::before {
            content: '';
            position: absolute;
            left: 10px;
            top: 8px;
            width: 12px;
            height: 12px;
            background: #667eea;
            border-radius: 50%;
            box-shadow: 0 0 0 4px rgba(102, 126, 234, 0.2);
        }
        
        .timeline-item::after {
            content: '';
            position: absolute;
            left: 15px;
            top: 20px;
            bottom: -30px;
            width: 2px;
            background: #e2e8f0;
        }
        
        .timeline-item:last-child::after {
            display: none;
        }
    </style>
</head>
<body>
    <!-- Hero Section -->
    <div class="hero-gradient text-white py-20 px-6">
        <div class="max-w-6xl mx-auto text-center relative z-10">
            <div class="mb-6">
                <i class="fas fa-database text-6xl opacity-80"></i>
            </div>
            <h1 class="text-5xl md:text-6xl font-bold mb-6" style="font-family: 'Noto Serif SC', serif;">
                Redis 持久化机制深度解析
            </h1>
            <p class="text-xl md:text-2xl opacity-90 max-w-3xl mx-auto leading-relaxed">
                探索 RDB 与 AOF 双重持久化策略的协同工作原理，掌握数据恢复的核心机制
            </p>
            <div class="mt-8 flex justify-center space-x-4">
                <span class="info-pill bg-white bg-opacity-20 text-white">
                    <i class="fas fa-clock mr-2"></i>阅读时间：8分钟
                </span>
                <span class="info-pill bg-white bg-opacity-20 text-white">
                    <i class="fas fa-layer-group mr-2"></i>深度技术文章
                </span>
            </div>
        </div>
    </div>

    <!-- Main Content -->
    <div class="max-w-6xl mx-auto px-6 py-12">
        <!-- Introduction Card -->
        <div class="content-card p-8 mb-12">
            <p class="text-lg leading-relaxed text-gray-700">
                <span class="drop-cap">当</span>Redis 同时开启 RDB 和 AOF 持久化时，服务重启时的加载过程优先使用 AOF 文件恢复数据，以确保数据的最新性和一致性。RDB 文件则作为备份，如果 AOF 文件不存在或损坏，可以作为备用方案。通过合理配置和优化持久化策略，可以在数据恢复和性能之间找到最佳平衡。
            </p>
        </div>

        <!-- Data Flow Visualization -->
        <div class="content-card p-8 mb-12">
            <h2 class="text-2xl font-bold mb-6 text-gray-800">
                <i class="fas fa-sitemap mr-3 text-purple-600"></i>Redis 数据恢复流程图
            </h2>
            <div class="mermaid">
                graph TD
                    A[Redis 启动] --> B{检查持久化文件}
                    B --> C{AOF 文件存在?}
                    C -->|是| D[优先加载 AOF]
                    C -->|否| E{RDB 文件存在?}
                    E -->|是| F[加载 RDB 文件]
                    E -->|否| G[空数据库启动]
                    D --> H{AOF 完整性检查}
                    H -->|通过| I[回放 AOF 命令]
                    H -->|失败| J[尝试修复 AOF]
                    J -->|成功| I
                    J -->|失败| K[回退到 RDB]
                    I --> L[数据恢复完成]
                    F --> L
                    K --> F
                    
                    style A fill:#667eea,stroke:#fff,stroke-width:2px,color:#fff
                    style L fill:#48bb78,stroke:#fff,stroke-width:2px,color:#fff
                    style D fill:#f56565,stroke:#fff,stroke-width:2px,color:#fff
            </div>
        </div>

        <!-- Section 1 -->
        <div class="content-card p-8 mb-12">
            <h2 class="section-title text-3xl font-bold mb-8 text-gray-800">
                1. 初始化和选择恢复策略
            </h2>
            <p class="text-lg leading-relaxed text-gray-700 mb-6">
                在 Redis 启动时，它会首先检查 RDB 和 AOF 文件的存在，并决定如何加载数据。Redis 的恢复策略主要取决于这两种文件的存在情况和相关配置。
            </p>

            <div class="grid md:grid-cols-2 gap-6">
                <div class="feature-card">
                    <h3 class="text-xl font-bold mb-4 text-gray-800">
                        <i class="fas fa-save mr-2 text-blue-600"></i>1.1 加载 RDB 文件
                    </h3>
                    <div class="space-y-4">
                        <div class="timeline-item">
                            <h4 class="font-semibold text-gray-700">存在 RDB 文件</h4>
                            <p class="text-gray-600">Redis 会首先检查是否存在有效的 RDB 文件（<code class="code-inline">dump.rdb</code>）。如果存在，Redis 会尝试加载这个 RDB 文件。</p>
                        </div>
                        <div class="timeline-item">
                            <h4 class="font-semibold text-gray-700">RDB 文件加载</h4>
                            <p class="text-gray-600">Redis 使用 <code class="code-inline">rdbLoad()</code> 函数从 RDB 文件中恢复数据。加载速度较快，因为只包含时间点快照。</p>
                        </div>
                    </div>
                </div>

                <div class="feature-card">
                    <h3 class="text-xl font-bold mb-4 text-gray-800">
                        <i class="fas fa-file-alt mr-2 text-green-600"></i>1.2 加载 AOF 文件
                    </h3>
                    <div class="space-y-4">
                        <div class="timeline-item">
                            <h4 class="font-semibold text-gray-700">存在 AOF 文件</h4>
                            <p class="text-gray-600">在加载 RDB 文件之后，Redis 会检查是否存在有效的 AOF 文件（<code class="code-inline">appendonly.aof</code>）。</p>
                        </div>
                        <div class="timeline-item">
                            <h4 class="font-semibold text-gray-700">AOF 文件加载</h4>
                            <p class="text-gray-600">Redis 使用 <code class="code-inline">aofLoad()</code> 函数来加载 AOF 文件，重播所有写操作命令。</p>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <!-- Highlight Box -->
        <div class="highlight-box mb-12">
            <h3 class="text-2xl font-bold mb-4">
                <i class="fas fa-lightbulb mr-3"></i>关键洞察
            </h3>
            <p class="text-lg leading-relaxed relative z-10">
                AOF 持久化通过记录每一条写命令来保证数据的完整性，而 RDB 则通过快照方式提供高效的数据备份。两者结合使用，既保证了数据安全，又提供了灵活的恢复选项。
            </p>
        </div>